package com.m3958.firstgwt.client.window;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.gwtutil.clientside.client.jso.VoteJso;
import com.m3958.gwtutil.clientside.client.jso.VoteTplJso;
import com.m3958.gwtutil.clientside.client.utils.ClientStringUtils;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

@Singleton
public class VoteScriptWindow extends Window{

	private boolean initialized = false;
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private ClientStringUtils csu;
	
	private DynamicForm df;
	
	
	public VoteScriptWindow(){
		setWidth(600);
		setHeight(450);
		setIsModal(true);
		setShowModalMask(true);
		setTitle("投票代码");
		centerInPage();
	}
	
	private Label usageLabel = new Label();
	private Label previewLabel = new Label();
	
	public VoteScriptWindow showMe(VoteJso vj,VoteTplJso vtj){
		if(!initialized){
			initialized = true;
			VLayout layout = new VLayout(5);
			Label l = new Label("模板使用方法：");
			l.setHeight(25);
			layout.addMember(l);
			usageLabel.setWidth100();
			usageLabel.setHeight(80);
			layout.addMember(usageLabel);
			layout.addMember(initResultForm());
			layout.addMember(previewLabel);
			previewLabel.setHeight(25);
			layout.addMember(initBt());
			addItem(layout);
		}
		show();
		setmeup(vj,vtj);
		return this;
	}
	

	private void setmeup(VoteJso vj, VoteTplJso vtj) {
		usageLabel.setContents(vtj.getTplUsage());
		String t = csu.yuisub(vtj.getOutScript(), prepareSubData(vj.getId(), vtj.getId(), vtj.getDspType()));
		df.setValue("resultScript", t);
		String pul = csu.yuisub(vtj.getPreviewUrl(), prepareSubData(vj.getId(), vtj.getId(), vtj.getDspType()));
		previewLabel.setContents("<a href=\"" + pul +  "\" target=\"_blank\"" + ">预览</a>");
	}
	
	private native JavaScriptObject prepareSubData(int voteid,int tplid,String output)/*-{
		return {voteid:voteid,tplid:tplid,output:output};
	}-*/;

	private DynamicForm initResultForm() {
		df = new DynamicForm();
		df.setWidth100();
		df.setHeight100();
		TextAreaItem ta = new TextAreaItem("resultScript", "引用代码");
		ta.setTitleOrientation(TitleOrientation.TOP);
		ta.setWidth(500);
		ta.setHeight(100);
		df.setFields(ta);
		return df;
	}

	private HLayout initBt() {
		HLayout hl = new HLayout();
		hl.setHeight(30);
		hl.setAlign(Alignment.RIGHT);
		IButton ib = new IButton("关闭");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				VoteScriptWindow.this.hide();
			}
		});
		hl.addMember(ib);
		return hl;
	}

}
